import React from "react";
import {
  Link,
  Routes,
  Route,
  useParams,
  useLocation,
  // useSearchParams,
} from "react-router-dom";

import useSearch from "./07-useSearch";

const Home = () => {
  // const location = useLocation();
  // console.log(location);
  // const [search] = useSearchParams();
  // console.log(search.get("a"));
  // console.log(window.location.search);

  const [search] = useSearch();
  console.log(search.get("a"));

  return <div>这是首页</div>;
};

const About = () => {
  const location = useLocation();
  console.log(location);
  return <div>这是关于页</div>;
};

const Detail = () => {
  const params = useParams();
  console.log(params);
  return <div>这是详情页 - {params.id}</div>;
};

const App = () => {
  return (
    <>
      <h2>react-router-v6-其他传参</h2>
      <ul>
        <li>
          <Link to="/home?a=3&b=4">首页</Link>
        </li>
        <li>
          <Link
            to={{
              pathname: "/about",
              search: "?c=5",
              hash: "#abc",
            }}
            // state要放在外面了
            state={{
              c: 10,
            }}
          >
            关于页
          </Link>
        </li>
        <li>
          <Link to="/detail/456">详情页</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        {/* 动态可选路由 */}
        {/* 将有参数的路由作为子路由 */}
        <Route path="/detail" element={<Detail />}>
          <Route path=":id" element={<Detail />}></Route>
        </Route>
        <Route path="*" element={<div>404 not found...</div>}></Route>
      </Routes>
    </>
  );
};

export default App;
